<!DOCTYPE html>
<html lang="en">
<head>
	<meta charset="UTF-8">
	<meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1">
	<title>扩展例子 | Elasticsearch: 权威指南 | Elastic</title>
    <!-- Give IE8 a fighting chance -->
    <!--[if lt IE 9]>
    <script src="https://oss.maxcdn.com/html5shiv/3.7.2/html5shiv.min.js"></script>
    <script src="https://oss.maxcdn.com/respond/1.4.2/respond.min.js"></script>
    <![endif]-->
	<link rel="stylesheet" type="text/css" href="../static/styles.css" />
</head>
<body>
<div class="main-container">
    <section id="content">
        
        <div class="content-wrapper">
            <section id="guide" lang="zh_cn">
                <div class="container">
                    <div class="row">
                        <div class="col-xs-12 col-sm-8 col-md-8 guide-section">
                            <div style="color:gray; word-break: break-all; font-size:12px;">原文地址: <a href="https://www.elastic.co/guide/cn/elasticsearch/guide/current/_extended_example.html" rel="nofollow">https://www.elastic.co/guide/cn/elasticsearch/guide/current/_extended_example.html</a>, 版权归 www.elastic.co 所有<br/>
                            英文版地址: <a href="https://www.elastic.co/guide/en/elasticsearch/guide/current/_extended_example.html" rel="nofollow">https://www.elastic.co/guide/en/elasticsearch/guide/current/_extended_example.html</a>
                            </div>
                        <!-- start body -->
                  <div class="page_header">
<b>请注意:</b><br>本书基于 Elasticsearch 2.x 版本，有些内容可能已经过时。
</div>
<div id="content">
<div class="breadcrumbs">
<span class="breadcrumb-link"><a href="index.html">Elasticsearch: 权威指南</a></span>
»
<span class="breadcrumb-link"><a href="aggregations.html">聚合</a></span>
»
<span class="breadcrumb-link"><a href="_looking_at_time.html">按时间统计</a></span>
»
<span class="breadcrumb-node">扩展例子</span>
</div>
<div class="navheader">
<span class="prev">
<a href="_returning_empty_buckets.html">« 返回空 Buckets</a>
</span>
<span class="next">
<a href="_the_sky_8217_s_the_limit.html">潜力无穷 »</a>
</span>
</div>
<div class="section">
<div class="titlepage"><div><div>
<h2 class="title">
<a id="_extended_example"></a>扩展例子<a class="edit_me edit_me_private" rel="nofollow" title="Editing on GitHub is available to Elastic" href="https://github.com/elasticsearch-cn/elasticsearch-definitive-guide/edit/cn/300_Aggregations/35_date_histogram.asciidoc">edit</a>
</h2>
</div></div></div>
<p>正如我们已经见过很多次的，buckets 可以嵌套进 buckets 中从而得到更复杂的分析。
比如，我们构建聚合以便按季度展示所有汽车品牌总销售额。同时按季度、按汽车品牌计算销售总额，以便可以找出哪种品牌最赚钱：</p>
<div class="pre_wrapper lang-sense">
<pre class="programlisting prettyprint lang-sense">GET /cars/transactions/_search
{
   "size" : 0,
   "aggs": {
      "sales": {
         "date_histogram": {
            "field": "sold",
            "interval": "quarter", <a id="CO195-1"></a><i class="conum" data-value="1"></i>
            "format": "yyyy-MM-dd",
            "min_doc_count" : 0,
            "extended_bounds" : {
                "min" : "2014-01-01",
                "max" : "2014-12-31"
            }
         },
         "aggs": {
            "per_make_sum": {
               "terms": {
                  "field": "make"
               },
               "aggs": {
                  "sum_price": {
                     "sum": { "field": "price" } <a id="CO195-2"></a><i class="conum" data-value="2"></i>
                  }
               }
            },
            "total_sum": {
               "sum": { "field": "price" } <a id="CO195-3"></a><i class="conum" data-value="3"></i>
            }
         }
      }
   }
}</pre>
</div>
<div class="sense_widget" data-snippet="snippets/300_Aggregations/35_date_histogram.json"></div>
<div class="calloutlist">
<table border="0" summary="Callout list">
<tr>
<td align="left" valign="top" width="5%">
<p><a href="#CO195-1"><i class="conum" data-value="1"></i></a></p>
</td>
<td align="left" valign="top">
<p>注意我们把时间间隔(<code class="literal">interval</code>)从 <code class="literal">month</code> 改成了 <code class="literal">quarter</code> 。</p>
</td>
</tr>
<tr>
<td align="left" valign="top" width="5%">
<p><a href="#CO195-2"><i class="conum" data-value="2"></i></a></p>
</td>
<td align="left" valign="top">
<p>计算每个品牌的总销售额。</p>
</td>
</tr>
<tr>
<td align="left" valign="top" width="5%">
<p><a href="#CO195-3"><i class="conum" data-value="3"></i></a></p>
</td>
<td align="left" valign="top">
<p>也计算所有品牌的总销售额。</p>
</td>
</tr>
</table>
</div>
<p>得到的结果（截去了一大部分）如下：</p>
<div class="pre_wrapper lang-js">
<pre class="programlisting prettyprint lang-js">{
....
"aggregations": {
   "sales": {
      "buckets": [
         {
            "key_as_string": "2014-01-01",
            "key": 1388534400000,
            "doc_count": 2,
            "total_sum": {
               "value": 105000
            },
            "per_make_sum": {
               "buckets": [
                  {
                     "key": "bmw",
                     "doc_count": 1,
                     "sum_price": {
                        "value": 80000
                     }
                  },
                  {
                     "key": "ford",
                     "doc_count": 1,
                     "sum_price": {
                        "value": 25000
                     }
                  }
               ]
            }
         },
...
}</pre>
</div>
<p>我们把结果绘成图，得到如 <a class="xref" href="_extended_example.html#date-histo-ts2" title="按品牌分布的每季度销售额">Figure 38, “按品牌分布的每季度销售额”</a> 所示的总销售额的折线图和每个品牌（每季度）的柱状图。</p>
<div id="date-histo-ts2" class="imageblock">
<div class="content">
<img src="../images/elas_29in02.png" alt="按品牌分布的每季度销售额">
</div>
<div class="title">Figure 38. 按品牌分布的每季度销售额</div>
</div>
</div>
<div class="navfooter">
<span class="prev">
<a href="_returning_empty_buckets.html">« 返回空 Buckets</a>
</span>
<span class="next">
<a href="_the_sky_8217_s_the_limit.html">潜力无穷 »</a>
</span>
</div>
</div>

                  <!-- end body -->
                        </div>
                        <div class="col-xs-12 col-sm-4 col-md-4" id="right_col">
                        
                        </div>
                    </div>
                </div>
            </section>
        </div>
    </section>
</div>
<script src="../static/cn.js"></script>
</body>
</html>